<!-- 新增 -->
<template>
  <el-dialog
    title="新增员工"
    width="40%"
    :visible="dialogVisible"
    @close="close"
    @open="fetchDepartment"
  >
    <el-form ref="addRef" :model="addForm" :rules="addRules" label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="addForm.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="addForm.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="addForm.timeOfEntry" placeholder="请选择入职时间" style="width: 100%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="addForm.formOfEmployment" placeholder="请选择聘用形式" style="width: 100%">
          <el-option v-for="item in hireList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="addForm.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- checkStrictly 属性设置为true可以实现选择任意一层级的数据 -->
        <!-- 属性设置为false 只能选中 最后一个 -->
        <!-- show-all-levels 是否在输入框中展示所有的层级 -->
        <!-- <el-cascader
          v-model="addForm.departmentName"
          style="width: 80%"
          :options="options"
          :props="defaultProps"
          :show-all-levels="false"
        /> -->
        <el-cascader
          v-model="addForm.departmentName"
          :options="options"
          :props="defaultProps"
          :show-all-levels="false"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="addForm.correctionTime" placeholder="请选择转正时间" style="width: 100%" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button size="small" @click="close">取消</el-button>
      <el-button size="small" type="primary" @click="submit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartment, addEmployee } from '@/api'
import findSon from '@/utils/findSon'
// import transTree from '@/utils/transTree'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

      options: [],
      // 部门级联选择器
      defaultProps: {
        value: 'name',
        label: 'name',
        checkStrictly: true
      },
      addForm: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      addRules: {
        username: [
          { required: true, message: '请输入姓名', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: ['blur', 'change'] }
        ]
      },
      hireList: [
        { id: 1, name: '正式' },
        { id: 2, name: '非正式' }
      ]
    }
  },
  methods: {
    close() {
      this.$emit('close')
      this.$refs.addRef.resetFields()
    },
    // 获取部门数据
    async fetchDepartment() {
      const res = await getDepartment()
      this.options = findSon(res.data.depts)
    },
    submit() {
      this.$refs.addRef.validate(async(valid) => {
        if (!valid) return
        await addEmployee({
          ...this.addForm,
          departmentName: this.addForm.departmentName[this.addForm.departmentName.length - 1]
        })
        this.$message.success('新增员工成功')
        this.close()
        this.$emit('add')
      })
    }
  }
}

</script>
<style lang='less' scoped>
</style>
